<template>
  <div class="article-page" >
    <nav class="my-nav van-hairline--bottom">
      <a href="javascript:;" :class="{ active: sorter==='weight_desc'}" @click="changeContent('weight_desc')">推荐</a>
      <a href="javascript:;" :class="{ active: sorter===null}" @click="changeContent(null)">最新</a>
      <div class="logo"><img src="@/assets/logo.png" alt=""></div>
    </nav>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <article-item :item="item" v-for="(item) in list" :key="item.id"></article-item>
    </van-list>
  </div>
</template>

<script>
import { articleAPI } from '@/api/article'

export default {
  name: 'ArticleView',
  data () {
    return {
      current: 1,
      sorter: 'weight_desc',
      list: [],
      loading: false,
      finished: false
    }
  },
  created () {
    // this.article()
  },
  methods: {
    async onLoad () {
      const { data: res } = await articleAPI({
        current: this.current,
        sorter: this.sorter
      })
      this.list.push(...res.data.rows)
      this.loading = false
      this.current++
      // console.log(res)
      if (this.current >= res.data.pageTotal) {
        this.finished = true
      }
    },
    changeContent (storter) {
      this.sorter = storter
      this.current = 1
      this.list = []
      this.loading = true // 避免重复发送请求(避免因滚动条问题, 自动调用onload)
      this.onLoad()
    }

    // onLoad () {
    //   // 异步更新数据
    //   // setTimeout 仅做示例，真实场景中一般为 ajax 请求
    //   setTimeout(() => {
    //     for (let i = 0; i < 10; i++) {
    //       this.list.push(this.list.length + 1)
    //     }
    //
    //     // 加载状态结束
    //     this.loading = false
    //
    //     // 数据全部加载完成
    //     if (this.list.length >= 40) {
    //       this.finished = true
    //     }
    //   }, 1000)
    // }
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
